#pragma once

#include <string>
#include <cstring>
#include <cstdio>
#include <cassert>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32

namespace AsyncLogger {
	namespace FileUtil {
		inline std::string getHostname() {
#ifdef _WIN32
			char hostname[MAX_COMPUTERNAME_LENGTH + 1];
			 DWORD size = sizeof(hostname);
			if (GetComputerNameA(hostname, &size)) {
				return hostname;
			}
#else
			char hostname[64];
			if (gethostname(hostname, sizeof(hostname)) == 0) {
				return hostname;
			}
#endif
			return "";
		};

		class AppendFile {
		public:
			AppendFile(const std::string& basename);

			~AppendFile();

			void append(const char* logline, size_t len);

			void flush();

			off_t writtenBytes() { return writtenBytes_; }
		private:
			size_t write(const char* logline, size_t len);

		private:
			off_t writtenBytes_;
			char buffer_[64 * 1024];
			FILE* fd_;
		};
	}
}